package com.chenjun.web.controller.sys;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chenjun.common.base.ResultDto;
import com.chenjun.common.bean.Pagination;
import com.chenjun.common.bean.TreeBean;
import com.chenjun.common.model.DataGridModel;
import com.chenjun.common.model.DataGridSortModel;
import com.chenjun.core.dao.generator.entity.SysResource;
import com.chenjun.core.service.sys.SysResourceService;
import com.chenjun.core.service.sys.SysRoleService;
import com.chenjun.core.vo.sys.ResourceDto;
import com.chenjun.core.vo.sys.TreeVo;
import com.chenjun.web.controller.BaseController;

/**
 * 
 * @Description: 菜单管理
 * @author yanjinyin
 * @date 2017年7月29日 上午10:12:35
 */
@SuppressWarnings("serial")
@Controller
@RequestMapping("resource")
public class ResourceController extends BaseController {
	
	private static final Logger logger = Logger.getLogger(ResourceController.class);

	@Autowired
	private SysResourceService sysResourceService;
	
	@Autowired
	private SysRoleService	sysRoleService;

	/**
	 * 页面展示
	 * @return
	 * ModelAndView
	 */
	@RequestMapping("/list.html")
	public ModelAndView index() {
		ModelAndView mv = new ModelAndView("resource/list");
		//补充业务逻辑
		return mv;
	}
	
	/**
	 * 分页搜索
	 * @param datagrid
	 * @return
	 * ResultDto
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	@RequestMapping(value = "/search")
	@ResponseBody
	public ResultDto list(DataGridModel datagrid) {
		ResultDto rtDto = new ResultDto();
		datagrid.getSortBy().add(new DataGridSortModel("sort_num", DataGridSortModel.ASC));
		Pagination data = sysResourceService.doPagination(datagrid);
		rtDto.setData(data);
		return rtDto;
	}

	/**
	 * 编辑页面元素
	 * @return
	 * ResultDto
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public ResultDto edit(ResourceDto dto)  throws IllegalAccessException, InvocationTargetException{
		ResultDto rtDto = new ResultDto();
		//重复验证
		DataGridModel dgm = new DataGridModel();
		dgm.getParams().put("nid", dto.getId());
		dgm.getParams().put("name",dto.getName());
		dgm.getParams().put("permissions",dto.getPermissions());
		dgm.getParams().put("type",dto.getType());
		List<SysResource> resources = sysResourceService.doListByCondition(dgm);
		if(!CollectionUtils.isEmpty(resources)){
			rtDto.resultError();
			rtDto.setMessage("菜单名称不能重复");
			return rtDto;
		}
		
		int i = sysResourceService.createOrUpdate(ResourceDto.getModelByDto(dto));
		if(i<=0){
			rtDto.resultError();
			rtDto.setMessage("编辑系统异常");
		}
		return rtDto;
	}

	/**
	 * 删除指定的记录
	 * TODO
	 * @param id
	 * @return
	 * ResultDto
	 */
	@RequestMapping("/delete/{id}")
	@ResponseBody
	public ResultDto delete(@PathVariable String id) {
		ResultDto rtDto = new ResultDto();
		SysResource resource = sysResourceService.get(id);
		int result = 0;
		if (resource != null) {
			result = sysResourceService.delete(id);
		}
		if (result <= 0) {
			rtDto.resultError();
			rtDto.setMessage("要删除的记录不存在!");
		}
		return rtDto;
	}

	/**
	 * 根据参数获取信息
	 * TODO
	 * @param id
	 * @return
	 * ResultDto
	 */
	@RequestMapping("/get/{id}")
	@ResponseBody
	public ResultDto get(@PathVariable String id) {
		ResultDto rtDto = new ResultDto();
		rtDto.setData(sysResourceService.get(id));
		return rtDto;
	}
	
	@RequestMapping("/getTreeResources")
	@ResponseBody
	@RequiresPermissions("role:base")
	public Object getAllResources(HttpServletRequest request) {
//		Role role = roleService.get(id);
		String treeId = request.getParameter("treeId") == null ? "" : request.getParameter("treeId");
		String disable = request.getParameter("disable") == null ? "false" : request.getParameter("disable");
		String pflag = request.getParameter("pflag") == null ? "false" : request.getParameter("pflag");
		DataGridModel dgm = new DataGridModel();
		dgm.setStart(0);
		dgm.setRows(0);
		Map<String, String> param = new HashMap<String, String>(2);
		param.put("treeId", treeId);
		param.put("disable", disable);
		param.put("pflag", pflag);
		dgm.getParams().putAll(param);
		List<TreeVo> tree = null;
		try {
			tree = sysResourceService.findAllResourcesForTree(dgm);
		} catch (Exception e) {
			logger.error(e, e);
			tree = new ArrayList<TreeVo>(0);
		}
		return tree;
	}

}
